package demo.liu.two.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author by Liujt
 * @Classname TestLoadBance
 * @Description TODO
 * @Date 2021/6/4 11:18
 */
@Slf4j
public class LoadBalanceTest {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        //loadBalanceTest(restTemplate);

        hystrixTest(restTemplate);
    }

    public static void hystrixTest(RestTemplate restTemplate) {
        String url = "http://127.0.0.1:9009/demoOne/test/feign";

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1, 2, 10,
                TimeUnit.MINUTES, new ArrayBlockingQueue(10000));

        int i = 0;
        while (true) {
            i++;
            System.out.println("--->" + i);
            threadPoolExecutor.submit(() -> run(0, restTemplate, url));
        }
    }

    public static void loadBalanceTest(RestTemplate restTemplate) {
        String url = "http://127.0.0.1:9009/demoOne/test/test?status=1";
        for (int i = 0; i < 10; i++) {
            run(i, restTemplate, url);
        }

    }



    public static void run(int i, RestTemplate restTemplate, String url) {
        long current = System.currentTimeMillis();
        String res = restTemplate.getForObject(url, String.class);
        System.out.println("请求时间" + (System.currentTimeMillis() - current));
        if (res.contains("失败熔断")) {
            System.out.println("_______失败熔断______");
        }
    }


}
